package com.webserver.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 *  用于处理客户端的交互
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public void run(){
        try {
            InputStream in = socket.getInputStream();
            int d;
            char pre = 'a';//记录上次读取的字符内容
            char cur;      //记录本次读取的字符内容
            StringBuilder builder = new StringBuilder();
            while ((d = in.read()) != -1){
                cur = (char)d;
                if (pre==13&&cur==10){
                    break;
                }
                builder.append(cur);
                pre=cur;//将本次读取的字符内容在下次循环时记录在上次读取的字符
            }
            String line = builder.toString().trim();
            System.out.println("请求行:"+line);

            //将读取到的请求行内容的三部分分别赋值给下面三个变量
            String method;//保存请求行中的请求方式
            String uri;//保存抽象路径
            String protocol;//保存协议版本
            String[] data = line.split("\\s");//按照空格拆分
            method = data[0];
            uri = data[1];
            protocol = data[2];
            System.out.println("method:"+method);
            System.out.println("uri:"+uri);
            System.out.println("protocol:"+protocol);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {

        }
    }
}